﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using LuaInterface;

namespace PengLuaKit
{
    //
    // Implement lua & unity communication
    // @lua     can send event to notify unity c#(lua -> unity)
    // @untiy   unity send event to lua excute lua function(unity -> lua)
    //
    public class PLKEventDispatcher
    {
        private static Dictionary<string, List<Action<System.Object>>> mEventCSharpListenr = new Dictionary<string, List<Action<System.Object>>>();
        private static Dictionary<string, List<LuaFunction>> mEventLuaListener = new Dictionary<string, List<LuaFunction>>();

        //
        // dispatch
        //
        public static void DispatcherEvent(string eventName, System.Object eventData = null)
        {
            List<Action<System.Object>> list;
            List<LuaFunction> list2;

            if (mEventCSharpListenr.TryGetValue(eventName, out list))
            {
                foreach (Action<System.Object> itemFun in list)
                {
                    if (itemFun != null)
                        itemFun(eventData);
                }
            }

            if (mEventLuaListener.TryGetValue(eventName, out list2))
            {
                foreach (LuaFunction item in list2)
                {
                    if (item != null)
                        item.Call(eventData);
                }
            }

        }

        //
        // add event listener
        //
        public static void AddCSharpEventListener(string eventName, Action<System.Object> OnEventListener)
        {
            List<Action<System.Object>> list;

            if (!mEventCSharpListenr.TryGetValue(eventName, out list))
            {
                list = new List<Action<System.Object>>();
                mEventCSharpListenr.Add(eventName, list);
            }
            list.Add(OnEventListener);
        }

        public static void AddLuaEventListener(string eventName, LuaFunction OnLuaEventListener)
        {
            List<LuaFunction> list2;

            if (!mEventLuaListener.TryGetValue(eventName, out list2))
            {
                list2 = new List<LuaFunction>();
                mEventLuaListener.Add(eventName, list2);
            }
            list2.Add(OnLuaEventListener);
        }

        //
        // remove event listener
        //
        public static void RemoveCSharpEventListener(string eventName, Action<System.Object> OnEventListener)
        {
            List<Action<System.Object>> list;

            if (mEventCSharpListenr.TryGetValue(eventName, out list))
            {
                int index = list.IndexOf(OnEventListener);
                if (index > 0)
                    list.Remove(OnEventListener);
            }
        }

        public static void RemoveLuaEventListener(string eventName, LuaFunction OnLuaEventListener)
        {
            List<LuaFunction> list2;

            if (mEventLuaListener.TryGetValue(eventName, out list2))
            {
                int index = list2.IndexOf(OnLuaEventListener);
                if (index > 0)
                    list2.Remove(OnLuaEventListener);
            }
        }

        public static void RemoveEventListener(string eventName)
        {
            List<Action<System.Object>> list;
            List<LuaFunction> list2;

            if (mEventCSharpListenr.TryGetValue(eventName, out list))
            {
                list.Clear();
            }
            if (mEventLuaListener.TryGetValue(eventName, out list2))
            {
                list2.Clear();
            }
        }

        public static void ClearEventListener()
        {
            mEventCSharpListenr.Clear();
            mEventLuaListener.Clear();
        }

    }


}